const Koa = require('koa');
const cors = require('koa2-cors')
const bodyparser = require('koa-bodyparser')
const app = new Koa();
const KoaRouter = require('koa-router')
const router = new KoaRouter()
const versionRouter = require('./router/version.js')
const fileRouter = require('./router/file.js')
// 可以把 POST 请求的参数解析到ctx.request.body
app.use(bodyparser({
    enableTypes:['json', 'form', 'text']
}))
// 解决跨域
app.use(cors(
    {
        credentials: true, // 允许跨域请求的情况下从其他域发送cookies
        allowHeaders: ['Content-Type', 'Authorization', 'Accept']
    }
))
// Routes
// 默认路由
router.get(`/`, async (ctx) => {
    ctx.body = 'welcome to jsLibs visual management';
})
app.use(router.allowedMethods()).use(router.routes());
app.use(versionRouter.routes(), versionRouter.allowedMethods())
app.use(fileRouter.routes(), fileRouter.allowedMethods())
const port = 5000
app.listen(port, ()=>{
    console.log('server is listening on ' + port)
});
